process.on('message', function(msg)
{
	console.log('child get message: ' + JSON.stringify(msg));
	try
	{
		let gulp = require("gulp");
		let rename = require('gulp-rename');
		let uglify = require("gulp-uglify-es").default;
		var options = msg; //JSON.stringify(msg);
		let startTime = Date.now();
		let path = require("path");
        let fs = require("fs");

		gulp.src(options.inputPath)
		.pipe(uglify({
			mangle: false
		}))
		.pipe(rename({ extname: '.min.js' }))
		.on('error', function (err) {
			process.send(err);
			errorFunc();
		})
		.pipe(gulp.dest(options.outputPath));
		
		let name = path.basename(options.inputPath);
		let minName = name.replace(/\.js$/, ".min.js");
		let minFilePath = path.join(options.outputPath, minName);
		let stat;
		let detectionTimer = setInterval(() => {
			if (fs.existsSync(minFilePath)) {
				stat = fs.statSync(minFilePath);
				let birthtime = stat.mtime || stat.ctime || stat.birthtime;
				if (birthtime > startTime) { // 在压缩以后创建的文件，默认为压缩成功
					successFunc();
				}
			}
		}, 3000);

		function successFunc() {
			detectionTimer && clearInterval(detectionTimer);
			process.exit(0);
		}
		function errorFunc() {
			detectionTimer && clearInterval(detectionTimer);
			process.exit(0);
		}
	}
	catch (e)
	{
		process.send(e);
	}
	// setTimeout(function(){
	// 	process.exit(1);
	// },1000)
	// process.exit(0);

});

// console.log('child pid: ' + process.pid);
// var UglifyJS = require('uglify-js');
// var fs = require("fs");
// process.on('message', function(msg)
// {
// 	console.log('child get message: ' + JSON.stringify(msg));
// 	try
// 	{
// 		// var option = JSON.stringify(msg);
// 		var options = msg//JSON.stringify(msg);
// 		var result = UglifyJS.minify([options.inputPath]);
// 		fs.writeFileSync(options.outputPath, result.code);
// 	}
// 	catch (e)
// 	{
// 		process.send(e);
// 	}
// 	// setTimeout(function(){
// 	// 	process.exit(1);
// 	// },1000)
// 	process.exit(0);

// });

// fs.writeFile("d:/dddd","asd");